package com.ly.honor.util;

import lombok.Data;

import java.io.Serializable;

/**
 * @author liyang
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 业务错误码
     */
    private long code;
    /**
     * 结果集
     */
    private T data;
    /**
     * 描述
     */
    private String msg;

    public Result() {
    }

    public static <T> Result<T> ok() {
        return restResult(null, ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
    }

    public static <T> Result<T> ok(T data) {
        return restResult(data, ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
    }

    public static <T> Result<T> ok(ResultEnum resultEnum) {
        return restResult(null, resultEnum.getCode(), resultEnum.getMsg());
    }

    public static <T> Result<T> ok(T data,ResultEnum resultEnum) {
        return restResult(data, resultEnum.getCode(), resultEnum.getMsg());
    }

    public static <T> Result<T> fail() {
        return restResult(null, ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
    }

    public static <T> Result<T> fail(T data) {
        return restResult(data, ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
    }

    public static <T> Result<T> fail(ResultEnum resultEnum) {
        return restResult(null, resultEnum.getCode(), resultEnum.getMsg());
    }

    public static <T> Result<T> fail(T data, ResultEnum resultEnum) {
        return restResult(data, resultEnum.getCode(), resultEnum.getMsg());
    }

    public Result<T> message(String msg){
        this.setMsg(msg);
        return this;
    }

    public Result<T> code(long code){
        this.setCode(code);
        return this;
    }


    private static <T> Result<T> restResult(T data, long code, String msg) {
        Result<T> apiResult = new Result<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }


}
